package cn.edu.zafu.bigdata.x.huangxihao202211080211.StudentManagementSystem11;

import java.util.Scanner;

public class StudentManagementSystem11 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        StudentManager11 manager = new StudentManager11();
        boolean running = true;

        while (running) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");

            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    addStudentInfo(manager, scanner);
                    break;
                case "2":
                    searchStudentInfo(manager, scanner);
                    break;
                case "3":
                    updateStudentInfo(manager, scanner);
                    break;
                case "4":
                    deleteStudentInfo(manager, scanner);
                    break;
                case "5":
                    running = false;
                    break;
                default:
                    System.out.println("无效选项，请重新输入！");
            }
        }

        System.out.println("程序已退出。");
        scanner.close();
    }

    private static void addStudentInfo(StudentManager11 manager, Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        if (manager.addStudent(id, name, phone)) {
            System.out.println("学生信息已添加！");
        } else {
            System.out.println("学号重复，添加失败！");
        }
    }

    private static void searchStudentInfo(StudentManager11 manager, Scanner scanner) {
        while (true) {
            System.out.print("请输入要检索的学号（输入 'QUIT' 退出）：");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            Student11 student = manager.searchStudent(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private static void updateStudentInfo(StudentManager11 manager, Scanner scanner) {
        while (true) {
            System.out.print("请输入要更新的学号（输入 'QUIT' 退出）：");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            System.out.print("请输入新姓名: ");
            String name = scanner.nextLine();
            System.out.print("请输入新电话: ");
            String phone = scanner.nextLine();

            if (manager.updateStudent(id, name, phone)) {
                System.out.println("学生信息已更新！");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private static void deleteStudentInfo(StudentManager11 manager, Scanner scanner) {
        while (true) {
            System.out.print("请输入要删除的学号（输入 'QUIT' 退出）：");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            if (manager.deleteStudent(id)) {
                System.out.println("学生信息已删除！");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }
}
